package com.seadragon.app.ksr.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.seadragon.app.ksr.model.Business;
import com.seadragon.app.ksr.model.KsrConstants;

public class BusinessAccessControlInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler) throws Exception {
		Business business = (Business)request.getSession().getAttribute(KsrConstants.SESSION_SCOPED_BUSINESS);
		String uri = request.getRequestURI();
		if (uri.equals("/ksr/business/signup.html")){
			if (business!=null){//user logged in
				response.sendRedirect("/ksr/business/profile.html");
				return false;			
			} else {
				return true;
			}
		} else if (uri.equals("/ksr/business/login.html")){
			if (business!=null){//user logged in
				response.sendRedirect("/ksr/business/index.html");
				return false;			
			} else {
				return true;
			}
		} else if (uri.equals("/ksr/business/profile.html")){
			if (business!=null){//user logged in
				return true;
			} else {
				response.sendRedirect("/ksr/business/signup.html");
				return false;			
			}
		} 
		return true;
    }

}
